//
//  ViewController.swift
//  webcd
//
//  Created by hebidu on 16/2/3.
//  Copyright © 2016年 Boye. All rights reserved.
//

import Cocoa
import WebKit

let MAIN_URL = "http://xiangce.itboye.com"

class ViewController: NSViewController ,WebUIDelegate,WebPolicyDelegate{

    @IBOutlet weak var mainWeb: WebView!
//    @IBOutlet weak var btnBack: NSButton!
    
    override func viewDidAppear() {
        super.viewDidAppear()
        
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        var width:CGFloat =  (NSScreen.mainScreen()?.frame.width)!;
        var height:CGFloat = (NSScreen.mainScreen()?.frame.height)!;
//        width = 1200
//        height = 180
        
        
        
        var mainWindow:NSViewController?  = self.presentingViewController
        
        mainWindow = self.parentViewController
        
          //DD
//        let window:NSWindow? = mainWindow?.title
        let title:String? = mainWindow?.title
        if(title != nil){
            NSLog("title=%@", title!)
        }
        NSLog("WIDTH=%f,height=%f",width,height)
        
        self.title = "客户端播放器";
        
        let strUrl:String = MAIN_URL;
        
        let url:NSURL? = NSURL(string: strUrl);
        
        let  request:NSURLRequest =  NSURLRequest(URL: url!)
        
        self.mainWeb.mainFrame.loadRequest(request)
        self.presentingViewController?.title = "123466";
        
        
        self.mainWeb.UIDelegate = self;
        self.mainWeb.policyDelegate = self;
        
//        self.btnBack.target = self;
//        self.btnBack.action = Selector("btnBackClick:")
        
        
    }
    
    override func prepareForSegue(segue: NSStoryboardSegue, sender: AnyObject?) {
        NSLog("prepareForSegue")
    }
    
//    func btnBackClick(sender:NSButton ){
//        self.mainWeb.goBack()
//    }
    
    override var representedObject: AnyObject? {
        didSet {
            
        }
    }
    
    
    func webView(sender: WebView!, drawHeaderInRect rect: NSRect) {
        NSLog("drawHeader In Rect");
    }
    
    func webView(webView: WebView!, decidePolicyForNavigationAction actionInformation: [NSObject : AnyObject]!, request: NSURLRequest!, frame: WebFrame!, decisionListener listener: WebPolicyDecisionListener!){
        NSLog("navigation action")
        if(request == nil){
            NSLog("requests nil")
            listener.ignore()
            return
        }
        let url:NSURL = request.URL!
        NSLog("%@", url.absoluteString)
        
        if(url.absoluteString.compare("about:blank") == NSComparisonResult.OrderedSame){
                        listener.ignore()
                        return
        }
//        
//        if(url == nil){
//            NSLog("requests URL nil")
//            listener.ignore()
//            return
//        }
        
        NSLog("%@", webView)
//        NSLog("%@", self)
        listener.use()
    }
    
    func webView(webView: WebView!, decidePolicyForNewWindowAction actionInformation: [NSObject : AnyObject]!, request: NSURLRequest!, newFrameName frameName: String!, decisionListener listener: WebPolicyDecisionListener!){
        NSLog("frameName = %@", frameName )
        if(frameName.compare("_blank") == NSComparisonResult.OrderedSame
            || frameName.compare("video") == NSComparisonResult.OrderedSame){
            //打开新窗口时，默认覆盖当前网页内容
            webView.mainFrame.loadRequest(request)
            listener.use()
        }
        
    }
    
    func webView(sender: WebView!, setStatusBarVisible visible: Bool) {
    
    }
    
    
}

